package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.Contract;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/contract")
public class ContractController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping("/list")
    public String list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {

        //创建example对象
        ContractExample example = new ContractExample();
        //通过example创建criteria对象
        ContractExample.Criteria criteria = example.createCriteria();
        //向criteria对象设置条件   where company_id=?
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        //根据当前登录用户的类型，构造不同的查询条件
        User user = getLoginUser();
        Integer degree = user.getDegree();

        if(degree == 4) {  //4-普通员工:查询自己签订的合同
            criteria.andCreateByEqualTo(user.getId());
        }else if(degree == 3) { //3-管理本部门：查询当前部门所有的合同
            criteria.andCreateDeptEqualTo(user.getDeptId());
        }else if(degree==2) { //2-管理所有下属部门和人员 : 根据部门id，进行模糊匹配
            criteria.andCreateDeptLike(user.getDeptId() +"%");
        }


        //设置排序 ORDER BY create_time DESC
        example.setOrderByClause("create_time DESC");


        PageInfo info = contractService.findAll(page, size, example);

        request.setAttribute("page", info);

        return "cargo/contract/contract-list";
    }


    @RequestMapping("/toAdd")
    public String toAdd() {
        return "cargo/contract/contract-add";
    }


    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        Contract contract = contractService.findById(id);
        request.setAttribute("contract",contract);
        return "cargo/contract/contract-update";
    }

    /**
     * 新增或者修改
     *  参数：contract对象
     *  返回：重定向
     */
    @RequestMapping("/edit")
    public String edit(Contract contract) {
        contract.setCompanyId(getLoginCompanyId());
        contract.setCompanyName(getLoginCompanyName());
        if(StringUtils.isEmpty(contract.getId())) {
            //设置合同的创建者信息（创建人id和创建人部门id）
            //1、查询当前登录的用户对象
            User user = getLoginUser();
            //2、设置创建人id
            contract.setCreateBy(user.getId());
            //3、设置创建人部门id
            contract.setCreateDept(user.getDeptId());
            contractService.save(contract);
        }else{
            contractService.update(contract);
        }
        return "redirect:/cargo/contract/list.do";
    }


    /**
     * 合同提交
     *   将合同状态由 0  -- 1
     *   请求连接：submit
     *   参数：合同id
     */
    @RequestMapping("/submit")
    public String submit(String id) {
        //1、根据id查询购销合同
        //2、判断合同状态是否 = 0
        //3、设置状态 1
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(1);
        contractService.update(contract);  // update tab set state=1 where id=?
        return "redirect:/cargo/contract/list.do";
    }


    /**
     * 合同取消
     */
    @RequestMapping("/cancel")
    public String cancel(String id) {
        //1、根据id查询购销合同
        //2、判断合同状态是否 = 0
        //3、设置状态 1
        Contract contract = new Contract();
        contract.setId(id);
        contract.setState(0);
        contractService.update(contract);  // update tab set state=1 where id=?
        return "redirect:/cargo/contract/list.do";
    }
}
